package com.lvmama.pangolin.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.entity.ConvertTask;
import com.lvmama.pangolin.proces.HotelDataProcess;
import com.lvmama.pangolin.service.ConvertTaskService;
@Component
@EnableScheduling
public class HotelDataFormatTask {
	/**  
	* @Title：DataFormatTask.java
	* @Author:zhoutianqi
	* @Description:定期扫描Timmer任务，转化数据
	* @Date：2017年2月13日下午8:07:25  
	*/
	private Logger logger = Logger.getLogger(HotelDataFormatTask.class);
	//酒店任务集合
	private static List<ConvertTask> hotelTaskList = new ArrayList<>();
	@Autowired
	private HotelDataProcess hotelDataProcess;
	@Autowired
	private ConvertTaskService taskService;
	@Value("#{'${taskLimitNum}'}")
	private String taskLimitNum;
	@Value("#{'${taskDefaultLimitNum}'}")
	private String taskDefaultLimitNum;
	
	@Scheduled(cron = "${hotel_data_format_cron}")
	public void scheduledMothod(){
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("status", Constant.DATA_RUNNING_STATE_CODE);
		paramMap.put("businessType", "HOTEL");
		int processNum = taskService.queryByMap(paramMap).size();
		if(processNum>0){return;}//线程中任务处理完，再缓存任务
		//清一下酒店
		hotelTaskList.clear();
		int limitNum = 0;
		if(taskLimitNum!=null && !taskLimitNum.equals("")){
			//获取指定条数的Timer
			limitNum=Integer.parseInt(taskLimitNum);
		}else{
			//获取default条数的Timer
			limitNum=Integer.parseInt(taskDefaultLimitNum);
		}
		paramMap.remove("status");
		paramMap.put("limitNum", limitNum);
		try {
			hotelTaskList = taskService.queryByLimitNum(paramMap);
			if(hotelTaskList==null || hotelTaskList.size()==0){return;}
			//更新酒店状态为20 正在处理
			Map<String,Object> param1 = new HashMap<String,Object>();
			for(ConvertTask convertTask:hotelTaskList){
				param1.put("status", Constant.DATA_RUNNING_STATE_CODE);
				param1.put("id", convertTask.getId());
				taskService.updateByMap(param1);
			}
			//起新线程处理酒店
			hotelDataProcess.executeProcess(hotelTaskList);
		} catch (Exception e) {
			logger.error("!!!!!!!!!!!!!DataFormatTask缓存任务异常!");
		}
	}
}
